package com.itheima.cloud.order.service;

import com.itheima.cloud.order.mapper.OrderMapper;
import com.itheima.cloud.pojo.Order;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
@Slf4j
public class OrderService {

    @Autowired
    OrderMapper orderMapper;


    /**
     * 根据用户名查询订单列表
     * @param username
     * @return
     */
    public List<Order> findOrderByUserId(String username) {
        // 1 参数校验
        if (StringUtils.isBlank(username)) {
            log.error("OrderService findOrderByUserId is called error by username is null");
            throw new RuntimeException("参数非法");
        }

        // 2 业务逻辑处理
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);

        // 3 封装处理结果集
        List<Order> orderList = orderMapper.selectByExample(example);
        if (orderList == null) {
            orderList = new ArrayList<>();
        }

       // int i = 1 / 0;

        return orderList;
    }

}
